package com.biscuit.page.basicservice.conf.config;

import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.biscuit.page.basicservice.conf.properties.RequestWhitelistProperties;
import com.biscuit.page.basicservice.conf.handler.MultipartFileSerializerHandler;
import com.biscuit.page.basicservice.interceptor.UserInfoInterceptor;
import com.biscuit.page.basicservice.interceptor.AuthorizationInterceptor;
import com.biscuit.page.basicservice.interceptor.CrossInterceptor;
import com.biscuit.page.basicservice.interceptor.RequestInfoInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author mazihao
 * @createTime 2024/3/7 16:59
 */
@Slf4j
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Resource
    private RequestWhitelistProperties requestWhitelistProperties;

    @Resource
    private RequestInfoInterceptor requestInfoInterceptor;

    @Resource
    private CrossInterceptor crossInterceptor;

    @Resource
    private AuthorizationInterceptor authorizationInterceptor;

    @Resource
    private UserInfoInterceptor userInfoInterceptor;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedHeaders("*")
                .exposedHeaders("*")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                .allowCredentials(true)
                .maxAge(3600);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(crossInterceptor).addPathPatterns("/**").order(1);
        registry.addInterceptor(userInfoInterceptor).addPathPatterns("/**").order(2);
        registry.addInterceptor(authorizationInterceptor)
                .order(3)
                .addPathPatterns("/**")
                .excludePathPatterns(
                        Stream.concat(
                                this.requestWhitelistProperties.getWhitelistUrlList().stream(),
                                this.requestWhitelistProperties.getStaticResourceList().stream()
                        ).collect(Collectors.toList())
                );
        registry.addInterceptor(requestInfoInterceptor).addPathPatterns("/**").order(4);
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.getSerializeConfig().put(CommonsMultipartFile.class, new MultipartFileSerializerHandler());
        // 添加Fastjson的配置信息
        fastConverter.setFastJsonConfig(fastJsonConfig);
        converters.add(fastConverter);
    }

}
